<?php
namespace Common\Model;
use Common\Model\VRNewsModel;
class AreaModel extends VRNewsModel
{
	
	protected $_validate = array(
	);

	
	//开通的地区列表
	function getEstablishList($keyword = null, $pageStart, $pageSize){
		$resultData = array();
		$where = array();
		$where['establish_status'] = array('EQ','1');
		$where['name'] = array('like','%'.$keyword.'%');
		$count_Total = $this->where($where)->count();
		$area_list = $this->where($where)
		->order("initial asc")
		->limit($pageStart, $pageSize)
		->select();
		$resultData['data'] = $area_list;
		$resultData['count_Total'] = $count_Total;
		return $resultData;
	}
	
	//获取所有的地区列表
	function getChooseAreaList($establish_status = false){
		$resultData = array();
		$where = array();
		if($establish_status) $where['establish_status'] = '1';
		$area_list = $this->where($where)
		->order("initial_group asc,initial asc")
		->select();
		$new_area_list = array();
		foreach($area_list as $key=>$value){
			$new_area_list[$value['initial_group']][] = $value;
		}
		
		foreach($new_area_list as $new_key=>$new_value){
			foreach($new_value as $new_key1=>$new_value1){
				$resultData[$new_key][$new_value1['initial']][] = $new_value1;
			}
		}
		
		return $resultData;
	}
	
	
	//地区下的管理员列表
	function getAdministrator($area_id = null){
		$Administrator_arr = array();
		if($area_id){
			$area_admin_model = M('vrnews1.admin','area_');
			$administrator_list = $area_admin_model
			->alias("a")
			->join("vradmin1.admin_user b on a.admin_id = b.admin_id")
			->where('a.area_id = '.$area_id .' and b.status = 1')
			->field("b.admin_id,b.username")
			->order("a.id desc")
			->select();
		}else{
			$admin_model = M('vradmin1.user','admin_');
			$administrator_list = $admin_model->where('type =2 and status =1')->field("admin_id,username")->select();
		}
		
		if($administrator_list){
			foreach($administrator_list as $key=>$value){
				if($value['admin_id'] && $value['username']) $Administrator_arr[$value['admin_id']] = $value['username'];
			}
		}
		return $Administrator_arr;
	}
	
	//开启/禁用
	function setDisableStatus($id = null, $status){
		if($id){
			$data['disable_status'] = $status;
			$rid = $this->where("area_id = ".$id)->data($data)->save();
			if($rid){
				return true;
			}
		}
		return false;
	}
	
	//地区删除
	function areaDel($area_id= null){
		if($area_id){
			$this->startTrans();
			$flag=false;
			$data['establish_status'] = 0;
			$hid = $this->where('area_id = '.$area_id)->data($data)->save();
			if($hid){
				$flag = true;
				//删除地区下的管理员
				$area_admin_model = M("vrnews1.admin","area_");
				$area_admin_count = $area_admin_model->where('area_id = '.$area_id)->count();
				if($area_admin_count > 0){
					$rid = $area_admin_model->where('area_id = '.$area_id)->delete();
				}else{
					$flag = true;
				}
				
				//同时 删除栏目对应的直播管理员
				$column_live = new ColumnLiveAdminModel();
				$have_live = $column_live->where('column_id = '.$area_id .' and type = 1')->select();
				if($have_live){
					$dellive = $column_live->where('column_id = ' . $area_id.' and type = 1')->delete();
				}else{
					$flag = true;
				}
				
				//删除地区对应的子公司
				$area_info = $this->where(['area_id'=>$area_id])->find();
				$company = M('vrnews1.company',null);
				$company_info = $company->where(['city'=>$area_info['name']])->find();
				$c_id = $company_info['company_id'];
				if($company_info){
					$company_id = $company->where(['city'=>$area_info['name']])->delete();
				}else{
					$flag = true;
				}
				//删除子公司下所有员工
				$personnel = M('vrnews1.personnel',null);
				$p_count = $personnel->where(['company_id'=>$c_id])->count();
				if($p_count > 0){
					$del_personnel = $personnel->where(['company_id'=>$c_id])->delete();
				}else{
					$flag = true;
				}
				if($rid && $dellive && $company_id && $del_personnel){
					$this->commit();
					$flag = true;
				}
			}
			if(!$flag){
				$this->rollback();
			}else{
				$this->commit();
				return true;
			}
		}
		return false;
	}
	
}

